package com.handa.documentManager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.handa.common.config.SnowflakeConfig;
import com.handa.common.entity.LoginUser;
import com.handa.common.entity.Response;
import com.handa.common.exception.ServiceException;
import com.handa.common.utils.RequestContext;
import com.handa.documentManager.entity.DO.UserFavoritesDO;
import com.handa.documentManager.service.UserFavoritesService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (UserFavorites)表控制层
 *
 * @author makejava
 * @since 2025-09-11 17:42:15
 */
@RestController
@RequestMapping("/documentManager/userFavorites")
public class UserFavoritesController {

    @Resource
    private SnowflakeConfig snowflakeConfig;

    /**
     * 服务对象
     */
    @Resource
    private UserFavoritesService userFavoritesService;


    @PostMapping("getAll")
    public Response<?> getAll(@RequestBody UserFavoritesDO userFavoritesDO) {
        return Response.success(this.userFavoritesService.list(new QueryWrapper<>(userFavoritesDO)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getById")
    public Response<?> selectOne(@RequestParam Long id) {
        return Response.success(this.userFavoritesService.getById(id));
    }

    @GetMapping("/getListByToken")
    public Response<?> getByToken() {
        LoginUser loginUser = RequestContext.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前登陆信息为空，请重新登陆");
        }

        QueryWrapper<UserFavoritesDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUser.getUserId());
        List<UserFavoritesDO> userFavoritesDOList = userFavoritesService.list(queryWrapper);
        return Response.success(userFavoritesDOList);
    }


    /**
     * 修改数据
     *
     * @param userFavoritesDO 实体对象
     * @return 修改结果
     */
    @PostMapping("/saveOrUpdate")
    public Response<?> saveOrUpdate(@RequestBody UserFavoritesDO userFavoritesDO) {
        LoginUser loginUser = RequestContext.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前登陆信息为空，请重新登陆");
        }
        if (userFavoritesDO.getId() == null) {
            userFavoritesDO.setId(snowflakeConfig.snowflakeId());
            userFavoritesDO.setUserId(loginUser.getUserId());
            userFavoritesDO.setCreatedAt(new Date());
        }

        userFavoritesDO.setUpdatedAt(new Date());

        try {
            userFavoritesService.saveOrUpdate(userFavoritesDO);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("已经存在同一桶下的同路径下的文件");
        }

        return Response.success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/deleteByIds")
    public Response<?> delete(@RequestParam("idList") List<Long> idList) {
        return Response.success(userFavoritesService.removeByIds(idList));
    }
}

